/**
 *
 */
package org.insia.smartposse.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.insia.smartposse.form.SearchProjectsForm;
import org.insia.smartposse.manager.ProjectsListManager;

/**
 * Class for manage the action when search an action
 *
 * @author cmougel
 *
 */
public class ProjectsAction extends DispatchAction {


	/**
	 * See all projects closed
	 *
	 * @param map
	 * @param form
	 * @param req
	 * @param resp
	 * @return
	 * @throws Exception
	 */
	public ActionForward listClose(ActionMapping map, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
		req.setAttribute("projects", new ProjectsListManager(ProjectsListManager.COMPLETE));
		req.setAttribute("all", true);
		return map.findForward("list");
	}

	/**
	 * Define the default value By list
	 */
	@Override
	protected ActionForward dispatchMethod(ActionMapping map, ActionForm form, HttpServletRequest req, HttpServletResponse resp, String name) throws Exception {
		if (null == name){
			return listOpen(map, form, req, resp);
		} else {
			return super.dispatchMethod(map, form, req, resp, name);
		}
	}

	/**
	 * View a list of All Project open
	 *
	 * @param map
	 * @param form
	 * @param req
	 * @param resp
	 * @return
	 * @throws Exception
	 */
	public ActionForward listOpen(ActionMapping map, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
		req.setAttribute("projects", new ProjectsListManager(ProjectsListManager.NOTCOMPLETE));
		return map.findForward("list");
	}


	/**
	 * For the Search with Subject Project
	 * @param map
	 * @param form
	 * @param req
	 * @param resp
	 * @return
	 * @throws Exception
	 */
	public ActionForward search(ActionMapping map, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
		SearchProjectsForm searchForm = (SearchProjectsForm) form;

		req.setAttribute("search", true);

		ProjectsListManager projList = new ProjectsListManager(searchForm.getStatus());
		projList.setSubject(searchForm.getSubject());
		projList.setShortDescription(searchForm.getShortDescription());

		req.setAttribute("projects",projList);
		return map.findForward("list");
	}



}
